<template>
  <el-container class="home-container">
    <el-header>
      <img src="../assets/heima.png" alt="">
      <span>电商后台管理系统</span>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px':'200px'" >
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
                :collapse-transition="false"
                :collapse="iscollapse"
                unique-opened
                router
                :default-active="activePath"
                background-color="#333744"
                text-color="#fff"
                active-text-color="#ffd04b">
          <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

export default {
  name: 'Home',
  data(){
    return {
      menulist: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activePath: '/users',
      iscollapse:false
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    },
    toggleCollapse(){
      this.iscollapse = !this.iscollapse
    },
    async getMenuList(){
      const {data: res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    }
  }
}
</script>

<style scoped>
  .toggle-button{
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #4a5064;
    color: #fff;
    letter-spacing: 0.2em;
    font-size: 10px;
    line-height: 24px;
  }
  .home-container{
    width: 100vw;
    height: 100vh;
  }
  .el-header{
    background-color: #373d41;
    display: flex;
    align-items: center;
  }
  .el-header img{
    position: absolute;
    left: 0;
  }
  .el-header span{
    margin-left: 60px;
    font-size: 20px;
    color: #fff;
  }
  .el-aside{
    background-color: #333744;
  }
  .el-main {
    background-color: #eaedf1;
  }
  .el-menu{
    border:none;
  }
</style>
